import datetime
from datetime import timedelta

#1.通过`datetime`模块完成: 时间戳, `datetime.datetime`对象, 格式化字符串三者之间的转换

# 获取当前时间
now = datetime.datetime.now(tz=None)
print(now)

# datetime.datetime -> str
now1 = now.strftime("%Y-%m-%d %H-%M-%S")
print(now1)

# datetime.datetime到timestamp
now2 = now.timestamp()
print(now2)

# timestamp到datetime.datetime
now3 = datetime.datetime.fromtimestamp(now2,tz=None)
print(now3)

# str 到 datetime.datetime
now4 = datetime.datetime.strptime(now1,"%Y-%m-%d %H-%M-%S")
print(now4)
print("------分隔符------")

# 2.封装一个函数`get_date(day_delta)`, 如果传入的是`-1` , 输出就是**字符串**日期`2020-01-02`.
def get_date(day_delta):
    now = datetime.datetime.now(tz=None)
    now_1 = now + timedelta(days=int(day_delta))
    result = now_1.strftime("%Y-%m-%d")
    return result

print("负整数,代表查询今天以前的日期."
      "正整数,代表查询今天以后的日期.")
day_delta = int(input("请输入你想查询的日期(整数):"))

if day_delta >= 0:
    print(day_delta,f'天后的日期为:', get_date(day_delta))
else:
    print(abs(day_delta), f'天前的日期为:', get_date(day_delta))
